<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Elec;

use App\Api\Controllers\BaseController;
use App\Models\Elec\ElecOpenAccountRecords;
use App\Models\Store;
use App\Services\ElectronicSignature\Bootstrap;
use App\Services\ElectronicSignature\ElectronicSignature;
use App\Services\ElectronicSignature\Strategy\EasypayElectronicSignatureHandler;
use App\Services\ElectronicSignature\Strategy\ElectronicSignatureInterface;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;

class OpenAccountController extends BaseController
{

    use ResponseTrait;


    //开户
    public function open(Request $request): string
    {
        try {
            //store_id
            $store_id = $request->post("store_id");
            if (empty($store_id)) {
                throw new \Exception("店铺ID不能为空");
            }
            //获取信息
            $store = Store::query()->where("store_id", $store_id)->first();
            if (empty($store)) {
                throw new \Exception("店铺不存在");
            }
            $elecSign = new ElectronicSignature(Bootstrap::run($request));
            $result = $elecSign->open($store);
            if (isset($result["open"]) && $result["open"] == 0) {
                throw new \Exception("开户失败原因：" . ($result["error"] ?? ""));
            }
            //分析结果
            return $this->successResponse("ok", [
                "user_id" => $result["user_id"]
            ]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function openInfo(Request $request): string
    {
        try {
            $store_id = $request->post("store_id");
            if (empty($store_id)) {
                throw new \Exception("店铺ID不能为空");
            }
            $store = Store::query()->where("store_id", $store_id)->first();
            if (empty($store)) {
                throw new \Exception("店铺不存在");
            }
            $api = $request->post("api", "easypay");
            //查询信息,进行开户
            $open = ElecOpenAccountRecords::query()->where([
                "api_type" => $api,
                "store_id" => $store_id,
            ])->first();
            $open_status = 0;
            if (!empty($open) && $open->open == 1) {
                $open_status = 1;
            }
            $response = [
                "open_status" => $open_status
            ];
            return $this->successResponse("ok", ["data" => $response]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }


    public function sms(Request $request): string
    {
        try {
            $store_id = $request->post("store_id");
            if (empty($store_id)) {
                throw new \Exception("店铺ID不能为空");
            }
            $store = Store::query()->where("store_id", $store_id)->first();
            if (empty($store)) {
                throw new \Exception("店铺不存在");
            }
            $elecSign = new ElectronicSignature(Bootstrap::run($request));
            $result = $elecSign->sms($store);
            return $this->successResponse("ok", $result);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

}